package com.hotel.jabc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Kaiser
 * @date 2018年10月5日 return jdbc的封装
 */
public class DbUtil {
	public static void main(String[] args) {
		DbUtil db = new DbUtil();
		try {
			db.setServerName("localhost");
			db.setDatebaseName("hotel");
			db.doQuery("");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// jdbc参数
	private String driverName = "com.mysql.jdbc.Driver";
	private String url = "jdbc:mysql://localhost:3306/hotel?characterEncoding=utf-8";
	private String user = "root";
	private String pwd = "1551246";

	private String serverName = "localhost";// 链接地址
	private int port = 3306;// 端口
	private String databaseName = "hotel";// 库名
	private String charset = "utf-8";// 编码格式
	private Connection con;

	public DbUtil() {
		this(null, null, null, null);
	}

	public DbUtil(String driverName) {
		this(driverName, null, null, null);
	}

	public DbUtil(String user, String pwd) {
		this(null, null, user, pwd);
	}

	public DbUtil(String url, String user, String pwd) {
		this(null, url, user, pwd);
	}

	public DbUtil(String driverName, String url, String user, String pwd) {
		if (driverName != null) {
			this.driverName = driverName;
		}
		if (url != null) {
			this.url = url;
		}
		if (user != null) {
			this.user = user;
		}
		if (pwd != null) {
			this.pwd = pwd;
		}
		// 执行加载驱动类
		init();
	}

	public void setDriverName(String driverName) {
		this.driverName = driverName;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	// 更改url方法
	public void updateUrl() {
		this.url = "jdbc:mysql://" + this.serverName + ":" + this.port + "/" + databaseName;
		System.out.println("url" + this.url);
	}

	public void setServerName(String serverName) {
		this.serverName = serverName;
		this.updateUrl();
	}

	public void setPort(int prot) {
		this.port = prot;
		this.updateUrl();
	}

	public void setDatebaseName(String databaseName) {
		this.databaseName = databaseName;
		this.updateUrl();
	}

	public void setCharset(String charset) {
		this.charset = charset;
		this.updateUrl();
	}

	// 执行查询sql语句
	public ResultSet doQuery(String sql, Object... params) throws SQLException {
		PreparedStatement pstat = this.getPreparedStatement(sql, params);
		return pstat.executeQuery();
	}

	// 加载驱动类方法
	public void init() {
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	// 连接对象方法

	// 连接对象
	public Connection getConnection() throws SQLException {
		this.con = DriverManager.getConnection(this.url, this.user, this.pwd);
		return this.con;
	}

	// 预处理的执行处理
	public PreparedStatement getPreparedStatement(String sql, Object[] params) throws SQLException {
		PreparedStatement pstat = this.getConnection().prepareStatement(sql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				pstat.setObject(i + 1, params[i]);
			}
		}
		return pstat;
	}

	// 关闭连接
	public void close() {
		try {
			this.con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 显示查询结果的方法
	 * 
	 * @throws SQLException
	 */
	public void showResultSet(ResultSet rs) throws SQLException {
		if (rs != null) {
			for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
				if (i == rs.getMetaData().getColumnCount()) {
					System.out.println();
				} else {
					System.out.print("\t");
				}
			}
			rs.beforeFirst(); // 将光标移动到第一行之前
			while (rs.next()) {
				for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
					System.out.print(rs.getObject(i));
					if (i == rs.getMetaData().getColumnCount()) {
						System.out.println();
					} else {
						System.out.print("\t");
					}
				}
			}
		}
	}
}
